﻿using System;
using System.Collections.Generic;
using Omniscient.Foundation.Contrib.Data.MySql;
using Omniscient.Foundation.Data;
using MySql.Data.MySqlClient;
using Omniscient.Foundation.ApplicationModel;

namespace Omniscient.TimeTracker.Data.Adapters.MySql
{
    public class ProjectAdapter : IEntityAdapter<Project>
    {
        IConnectivity _connectivity;

        public ProjectAdapter() : this(ApplicationManager.Current.ServiceProvider.GetService<IConnectivity>()) { }

        public ProjectAdapter(IConnectivity connectivity)
        {
            if (connectivity == null) throw new ArgumentNullException("The adapter cannot work with no connectivity.  Null argument: connectivity.");
            _connectivity = connectivity;
        }

        #region IEntityAdapter<Project> Members

        public EntityList<Project> LoadByObjectQuery(Omniscient.Foundation.Data.ObjectQuery.OQuery<Project> query)
        {
            throw new NotImplementedException("Not implemented yet.");
        }

        public Project LoadByKey(Guid id)
        {
            MySqlConnection cn;
            MySqlCommand cmd;
            MySqlDataReader rd;

            cn = _connectivity.CreateConnection();

            string sql = string.Format(@"SELECT Name, ClientGuid, StartDate, EndDate, Description 
                            FROM project
                            WHERE ProjectGuid = '{0}'", id.ToString("D"));

            try
            {
                cmd = cn.CreateCommand();
                cmd.CommandText = sql;
                cn.Open();
                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    Project p;
                    p = new Project(id, true);

                    Guid clientId;
                    clientId = new Guid(rd.GetString("ClientGuid"));

                    p.ParentClient = new Client(clientId, false);
                    p.Name = rd.GetString("Name");
                    p.StartDate = rd.GetDateTime("StartDate");
                    p.EndDate = rd.GetDateTime("EndDate");
                    p.Description = rd.GetString("Description");

                    rd.Close();
                    return p;
                }
                return null;
            }
            finally
            {
                cn.Dispose();
            }
        }

        public void Save(Project entity)
        {
            throw new NotImplementedException("Not implemented yet.");
        }

        public EntityList<Project> LoadAll()
        {
            MySqlConnection cn;
            MySqlCommand cmd;
            MySqlDataReader rd;
            EntityList<Project> projects;

            projects = new EntityList<Project>();
            cn = _connectivity.CreateConnection();

            string sql = @"SELECT ProjectGuid, Name, ClientGuid, StartDate, EndDate, Description FROM project";

            try
            {
                cmd = cn.CreateCommand();
                cmd.CommandText = sql;
                cn.Open();
                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    Project p;
                    Guid id;
                    id = new Guid(rd.GetString("ProjectGuid"));
                    p = new Project(id, true);

                    Guid clientId;
                    clientId = new Guid(rd.GetString("ClientGuid"));

                    p.ParentClient = new Client(clientId, false);
                    p.Name = rd.GetString("Name");
                    p.StartDate = rd.GetDateTime("StartDate");
                    p.EndDate = rd.GetDateTime("EndDate");
                    p.Description = rd.GetString("Description");
                    projects.Add(p);
                }
                rd.Close();
                return projects;
            }
            finally
            {
                cn.Dispose();
            }
        }

        public EntityList<Project> LoadByForeignKey(string propertyName, Guid id)
        {
            throw new NotImplementedException();
        }

        public EntityList<Project> LoadByQuery(string queryName)
        {
            throw new NotImplementedException();
        }

        public EntityList<Project> LoadByValueProperty(string propertyName, object value)
        {
            throw new NotImplementedException();
        }

        public void Save(IEnumerable<Project> entities)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
